<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>课程列表</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
    <style type="text/css">
        th, td {
            white-space: nowrap;
            overflow: hidden;
            word-break: keep-all;
        }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#menubar(activeUri1='课程管理',activeUri2='课程列表')"></div>

    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <h1>课程信息管理</h1>
                    <hr>
                    <div class="form-inline col-sm-12">
                        <label class="control-label col-sm-1" for="selectcoursetype"><strong>类型</strong></label>
                        <select id="selectcoursetype" class="form-control form-control-sm col-sm-3"
                                data-live-search="true">
                            <option value="" selected="selected">请选择</option>
                            <option value="7">必修课</option>
                            <option value="0">专业选修课</option>
                            <option value="1">高新技术类</option>
                            <option value="2">跨学科领域类</option>
                            <option value="3">数学与自然科学类</option>
                            <option value="4">人文与社会科学类</option>
                            <option value="5">中华文明与外国文化类</option>
                            <option value="6">礼仪修养与语言表达类</option>
                        </select>
                        <label class="control-label col-sm-1" for="selectcredits"><strong>学分</strong></label>
                        <select id="selectcredits" class="form-control form-control-sm col-sm-3"
                                data-live-search="true">
                            <option value="-1" selected="selected">请选择</option>
                            <option value="1">1学分</option>
                            <option value="2">2学分</option>
                            <option value="3">3学分</option>
                            <option value="4">4学分</option>
                        </select>
                        <label class="control-label col-sm-1" for="selecttime"><strong>学时</strong></label>
                        <input type="number" id="selecttime" class="form-control form-control-sm col-sm-3">
                    </div>
                    <hr>
                    <div class="fixed-table-toolbar">
                        <div class="bs-bars pull-left">
                            <div id="toolbar" class="btn-group">
                                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal"
                                        data-target="#importCourseModal">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 导入课程
                                </button>
                                <button type="button" class="btn btn-info btn-sm" data-toggle="modal"
                                        data-target="#addCourseModal">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 添加课程
                                </button>
                                <button type="submit" class="btn btn-danger btn-sm" data-toggle="modal"
                                        id="deleteCheckBtn" disabled="disabled">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 删除选中
                                </button>
                            </div>
                        </div>
                        <div class="columns columns-right btn-group pull-right">
                            <div class="keep-open btn-group" title="列">
                                <button class="btn btn-default" type="button" name="refresh" title="刷新"
                                        onclick="myrefresh()">
                                    <i class="fa fa-refresh"></i></button>
                                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                    <i class="fas fa-table"></i><span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <input type="checkbox" checked="checked" id="td1" onclick="hiddenTD('td1')">ID
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td2" onclick="hiddenTD('td2')">名称
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td3" onclick="hiddenTD('td3')">类型
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td4" onclick="hiddenTD('td4')">学时
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td5" onclick="hiddenTD('td5')">学分
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td6" onclick="hiddenTD('td6')">开始选报时间
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td7" onclick="hiddenTD('td7')">结束选报时间
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td8" onclick="hiddenTD('td8')">描述
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td9" onclick="hiddenTD('td9')">操作
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div id="addCourseModal" class="modal fade" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel"
                         data-backdrop="static" data-keyboard="false">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title">添加课程</h4>
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-label="Close"><span
                                            aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <table align="center" class="table">
                                        <tr>
                                            <td>名称</td>
                                            <td>
                                                <input type="text" id="addCourseName">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>ID</td>
                                            <td>
                                                <input type="text" id="addCourseId">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>类型</td>
                                            <td>
                                                <select id="addCourseType">
                                                    <option value="7">必修课</option>
                                                    <option value="0">专业选修课</option>
                                                    <option value="1">高新技术类</option>
                                                    <option value="2">跨学科领域类</option>
                                                    <option value="3">数学与自然科学类</option>
                                                    <option value="4">人文与社会科学类</option>
                                                    <option value="5">中华文明与外国文化类</option>
                                                    <option value="6">礼仪修养与语言表达类</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>学时</td>
                                            <td>
                                                <input type="number" id="addTime">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>学分</td>
                                            <td>
                                                <select id="addCredits">
                                                    <option value="1">1学分</option>
                                                    <option value="2">2学分</option>
                                                    <option value="3">3学分</option>
                                                    <option value="4">4学分</option>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>开始选报时间</td>
                                            <td>
                                                <input type="date" id="addBeginDate"><br/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>结束选报时间</td>
                                            <td>
                                                <input type="date" id="addEndDate"><br/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>描述</td>
                                            <td>
                                                <textarea rows="2" cols="24" id="addInfo"></textarea>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <button id="submitAdd" type="button" class="btn btn-primary">添加</button>
                                    <button type="button" class="btn btn btn-secondary" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div id="importCourseModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel" data-backdrop="static" data-keyboard="false">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title">导入课程</h4>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                            aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <form id="importForm" class="form-horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">格式</label>
                                            <div class="col-sm-10">
                                                <img th:src="@{/img/helpcourse.png}" width="765px">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">选择文件</label>
                                            <div class="col-sm-10">
                                                <input id="file" name="file" type="file" class="file"
                                                       data-show-preview="false"
                                                       data-show-upload="false"
                                                       accept="application/vnd.sealed-xls, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button id="submitImport" type="button" class="btn btn-primary">导入</button>
                                    <button type="button" class="btn btn btn-secondary" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="fixed-table-container table-responsive">
                        <table class="table table-hover table-striped table-bordered" id="datas">
                            <thead>
                            <th style="max-width: 10px"><input type="checkbox" class="checkall"
                                                               onclick="mydeleteallcheck(this.checked)"></th>
                            <th class="td1">ID</th>
                            <th class="td2">名称</th>
                            <th class="td3" hidden="hidden">类型</th>
                            <th class="td4">学时</th>
                            <th class="td5">学分</th>
                            <th class="td6" hidden="hidden">开始选报时间</th>
                            <th class="td7" hidden="hidden">结束选报时间</th>
                            <th class="td8" hidden="hidden">介绍</th>
                            <th class="td9">操作</th>

                            </thead>
                            <tbody id="mytbody">
                            <tr id="template">
                                <td class="td0"></td>
                                <td class="td1"></td>
                                <td class="td2"></td>
                                <td class="td3" hidden="hidden"></td>
                                <td class="td4"></td>
                                <td class="td5"></td>
                                <td class="td6" hidden="hidden"></td>
                                <td class="td7" hidden="hidden"></td>
                                <td class="td8" hidden="hidden"></td>
                                <td class="td9"></td>
                            </tr>
                            </tbody>
                        </table>
                        <br/>
                        <div>
                            <div class="pull-left">
                                <input type="hidden" id="offset" value="0">
                                显示第<span id="begin"></span>到第<span id="end"></span>条记录，总共<span id="sum"></span>条记录。
                                每页显示
                                <select id="limit" onchange="this.allCourseIds = changeLimit(this.allCourseIds)">
                                    <option value="5">5</option>
                                    <option value="10" selected="selected">10</option>
                                    <option value="15">15</option>
                                    <option value="30">30</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select> 条记录
                            </div>
                            <div class="pull-right">
                                <nav aria-label="Page navigation">
                                    <ul class="pagination" id="pages"></ul>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="updateCourseModal" tabindex="-1"
                     role="dialog" aria-labelledby="studentModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title"
                                    id="courseModalLabel">详细信息</h5>
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table align="center">
                                    <tr>
                                        <td>名称</td>
                                        <td>
                                            <input type="text" id="courseName">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>ID</td>
                                        <td>
                                            <input type="text" id="courseId">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>类型</td>
                                        <td>
                                            <select id="courseType">
                                                <option value="7">必修课</option>
                                                <option value="0">专业选修课</option>
                                                <option value="1">高新技术类</option>
                                                <option value="2">跨学科领域类</option>
                                                <option value="3">数学与自然科学类</option>
                                                <option value="4">人文与社会科学类</option>
                                                <option value="5">中华文明与外国文化类</option>
                                                <option value="6">礼仪修养与语言表达类</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>学时</td>
                                        <td>

                                            <input type="number" id="time">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>学分</td>
                                        <td>
                                            <select id="credits">
                                                <option value="1">1学分</option>
                                                <option value="2">2学分</option>
                                                <option value="3">3学分</option>
                                                <option value="4">4学分</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>开始选报时间</td>
                                        <td>
                                            <input type="date" id="beginDate"><br/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>结束选报时间</td>
                                        <td>
                                            <input type="date" id="endDate"><br/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>描述</td>
                                        <td>
                                            <textarea rows="2" cols="24" id="info"></textarea>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-info" id="submitUpdate">保存修改</button>
                                <button type="button" class="btn btn-danger" id="submitDelete">删除课程</button>
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button>
                            </div>
                        </div>
                    </div>
                </div>
                <footer>
                    <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
                </footer>
            </div>
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>
    var allCourseIds = [];

    var limit;
    var begin = 1;
    var end = 0;
    var pageNum;
    var currentPage = 1;

    $(function () {
        limit = $("#limit option:selected").val();
        TableInit();
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function () {
        sendAjax('/admin/api/course', 'get', {
            limit: $("#limit option:selected").val(),
            offset: $("#offset").val(),
            coursetype: $("#selectcoursetype option:selected").val(),
            credits: $("#selectcredits option:selected").val(),
            time: $("#selecttime").val() == "" ? -1 : $("#selecttime").val()
        }, function (res) {
            paging(res);
            $.each(res.data, function (i, course) {
                var row = $("#template").clone();
                row.find(".td0").html('<input type="checkbox" class="checks" onclick="mydeletecheck(' + course.courseid + ')">');
                row.find(".td1").text(course.courseid);
                row.find(".td2").text(course.coursename);
                row.find(".td3").text(switchCoursetype(course.coursetype));
                row.find(".td4").text(course.time);
                row.find(".td5").text(course.credits);
                row.find(".td6").text(course.begindate.substring(0, 10));
                row.find(".td7").text(course.enddate.substring(0, 10));
                row.find(".td8").text(course.info);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#updateCourseModal" onclick="updateCourse(\'' + course.courseid + '\')">详情</button>';
                row.find(".td9").html(html);
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }

    function myrefresh() {
        $("#mytbody").children().remove();
        $("#mytbody").html('<tr id="template">\n' +
            '                                <td class="td0"></td>\n' +
            '                                <td class="td1"></td>\n' +
            '                                <td class="td2"></td>\n' +
            '                                <td class="td3" hidden="hidden"></td>\n' +
            '                                <td class="td4"></td>\n' +
            '                                <td class="td5"></td>\n' +
            '                                <td class="td6" hidden="hidden"></td>\n' +
            '                                <td class="td7" hidden="hidden"></td>\n' +
            '                                <td class="td8" hidden="hidden"></td>\n' +
            '                                <td class="td9"></td>\n' +
            '                            </tr>');
        TableInit();
        hiddenTD("td3");
        hiddenTD("td6");
        hiddenTD("td7");
        hiddenTD("td8");
    }

    function updateCourse(courseid) {
        sendAjax('/admin/api/course/' + courseid, 'get', {}, function (res) {
            $("#courseName").val(res.data.coursename);
            $("#courseId").val(res.data.courseid);
            $("#courseType option").each(function () {
                if ($(this).val() === res.data.coursetype) {
                    $(this).prop("selected", true);
                }
            });
            $("#time").val(res.data.time);
            $("#credits").val(res.data.credits);
            $("#beginDate").val(res.data.begindate.substring(0, 10));
            $("#endDate").val(res.data.enddate.substring(0, 10));
            $("#info").val(res.data.info);
        }, '');
    }

    var ButtonGroup = function () {
        var object = {};


        object.init = function () {

            $('#submitImport').click(function () {
                if (confirm("确定导入课程？")) {
                    var formData = new FormData($("#importForm")[0]);
                    uploadFile("/admin/api/course/import", "post", formData, function (res) {
                        alert("导入成功");
                        $('#importCourseModal').modal('hide');
                        myrefresh();
                    }, "#submitImport");
                }
            });

            $('#submitAdd').click(function () {
                let date = new Date();
                let today = new Date(date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate());
                if (confirm('确认新增课程？')) {
                    sendAjax('/admin/api/course', 'post', {
                        courseid: $("#addCourseId").val(),
                        coursetype: $("#addCourseType option:selected").val(),
                        coursename: $("#addCourseName").val(),
                        info: $("#addInfo").val(),
                        credits: $("#addCredits").val(),
                        time: $("#addTime").val(),
                        begindate: $("#addBeginDate").val() == "" ? today : new Date($("#addBeginDate").val()),
                        enddate: $("#addEndDate").val() == "" ? today : new Date($("#addEndDate").val()),
                    }, function (res) {
                        alert("发布成功！");
                        $('#addCourseModal').modal('hide');
                        myrefresh();
                    }, "#submitAdd");
                }
            });

            $("#submitUpdate").click(function submitUpdate() {
                sendAjax('/admin/api/course', 'put', {
                    coursename: $("#courseName").val(),
                    courseid: $("#courseId").val(),
                    coursetype: $("#courseType option:selected").val(),
                    time: $("#time").val(),
                    credits: $("#credits").val(),
                    begindate: new Date($("#beginDate").val()),
                    enddate: new Date($("#endDate").val()),
                    info: $("#info").val(),
                }, function (res) {
                    alert("修改成功！");
                    $('#updateCourseModal').modal('hide');
                    myrefresh();
                }, "#submitUpdate");
            });

            $('#submitDelete').click(function () {
                if (confirm('确认删除公告？')) {
                    sendAjax('/admin/api/course/' + $("#courseId").val(), 'delete', {}, function (res) {
                        alert("删除成功！");
                        $('#updateCourseModal').modal('hide');
                        myrefresh();
                    }, "#submitDelete");
                }
            });

            $('#deleteCheckBtn').click(function () {
                if (confirm('确认删除选中课程？')) {
                    sendAjax('/admin/api/course/list', 'delete', {
                        allCourseIds: JSON.stringify(allCourseIds),
                    }, function (res) {
                        alert("删除成功！");
                        allCourseIds = [];
                        $(".checkall").prop("checked", false);
                        $("#deleteCheckBtn").prop("disabled", "disabled");
                        myrefresh();
                    }, "#deleteCheckBtn");
                }
            });

        };

        return object;
    };

    function mydeleteallcheck(courseid) {
        this.allCourseIds = deleteallcheck(courseid, allCourseIds);
    }

    function mydeletecheck(courseid) {
        this.allCourseIds = deletecheck(courseid, allCourseIds);
    }
</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>

<!-- Slimscroll JS -->
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>

<!-- Custom JS -->
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
